package com.sanhuo.idms.dao.provider;

import com.sanhuo.idms.bo.RepairSearchBO;
import com.sanhuo.idms.enums.RepairStateEnum;
import com.sanhuo.triild.provider.SQL;
import com.sanhuo.utils.basic.StringUtil;

import java.util.ArrayList;

public class RepairProvider extends SQL {
    public String page(RepairSearchBO bo) {
        SQL sql = getSQL();
        sql.before(" select concat(t.num ,\"-\",rm.num) dorm,r.device,r.content,r.state,r.updateTime,r.id ,u.name creator from " +
                "       repair r join user u join dorm d join tung t " +
                "           join room rm on r.dormId = d.id  and d.roomId = rm.id and d.tungId = t.id and u.id = r.creatorId ");
        if (StringUtil.isNotBlank(bo.getUserId())) {
            sql.add(" where 1=1 ");
        }
        if (StringUtil.isNotBlank(bo.getUserId())) {
            sql.where(" r.creatorId = #{userId} ");
        }
        return sql.build();
    }

    public String findByIds(ArrayList<String> ids) {
        SQL sql = getSQL();
        sql.before("select concat(t.num ,\"-\",rm.num) dorm,r.device,r.content,r.state,r.id  from repair r join dorm d join tung t join room rm " +
                "on r.dormId = d.id  and d.roomId = rm.id and d.tungId = t.id");
        sql.add(" where r.id in (");
        for (int i = 0; i < ids.size(); i++) {
            sql.add(" #{id" + i + "},");
        }
        String zhongSQL = sql.build();
        sql = getSQL().before(zhongSQL.substring(0, zhongSQL.lastIndexOf(",")));
        sql.add(" )");
//        sql.where(" r.state = " + RepairStateEnum.INPROCESSING.getCode());
//        System.out.println(sql.build());
        return sql.build();
    }
}
